﻿namespace Yom.Data
{
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    /// <summary>
    /// 友情链接数据操作
    /// 
    /// 创建: 邱柯皓 2009/02/04 10:24
    /// </summary>
    public class Link
    {
        #region 私有成员定义
        private readonly DBHelper db = new DBHelper();
        #endregion

        #region 构造函数
        public Link() { }
        #endregion

        #region 私有方法
        /// <summary>
        /// 通过DataReader获取友情链接对象
        /// 
        /// 创建: 邱柯皓 2009/02/04 10:26
        /// </summary>
        /// <param name="dr">DataReader对象</param>
        /// <returns>返回友情链接实体对象</returns>
        private Yom.Model.Link GetModel(SqlDataReader dr)
        {
            Yom.Model.Link model = new Yom.Model.Link();
            model.IsCheck = db.GetBool(dr["IsCheck"]);
            model.LinkID = db.GetInt(dr["LinkID"]);
            model.LinkName = db.GetString(dr["LinkName"]);
            model.LinkUrl = db.GetString(dr["LinkUrl"]);

            return model;
        }

        /// <summary>
        /// 通过DataReader获取友情链接列表
        /// 
        /// 创建: 邱柯皓 2009/02/04 10:28
        /// </summary>
        /// <param name="dr">DataReader对象</param>
        /// <returns>返回友情链接列表</returns>
        private List<Yom.Model.Link> GetList(SqlDataReader dr)
        {
            List<Yom.Model.Link> list = new List<Yom.Model.Link>();

            while (dr.Read())
                list.Add(GetModel(dr));

            return list;
        }
        #endregion

        #region 公共方法
        /// <summary>
        /// 添加友情链接
        /// 
        /// 创建: 邱柯皓 2009/02/04 10:31
        /// </summary>
        /// <param name="model">友情链接实体对象</param>
        /// <returns>返回成功标识</returns>
        public bool Add(Yom.Model.Link model)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append("INSERT INTO [Yom_Link] (");
            strSQL.Append("[LinkName], [LinkUrl], [IsCheck]");
            strSQL.Append(") VALUES (");
            strSQL.Append("@LinkName, @LinkUrl, @IsCheck)");
            SqlParameter[] cmdParms = {
                db.CreateInSqlParameter("@LinkName", DbType.String, model.LinkName),
                db.CreateInSqlParameter("@LinkUrl", DbType.String, model.LinkUrl),
                db.CreateInSqlParameter("@IsCheck", DbType.Boolean, model.IsCheck)};

            return db.ExecuteNonQuery(CommandType.Text, strSQL.ToString(), cmdParms) > 0;
        }

        /// <summary>
        /// 修改友情链接
        /// 
        /// 创建: 邱柯皓 2009/02/04 10:35
        /// </summary>
        /// <param name="model">友情链接实体对象</param>
        /// <returns>返回成功标识</returns>
        public bool Update(Yom.Model.Link model)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append("UPDATE [Yom_Link] SET ");
            strSQL.Append("[LinkName]=@LinkName, [LinkUrl]=@LinkUrl, [IsCheck]=@IsCheck ");
            strSQL.Append("WHERE [LinkID]=@LinkID");
            SqlParameter[] cmdParms = {
                db.CreateInSqlParameter("@LinkName", DbType.String, model.LinkName),
                db.CreateInSqlParameter("@LinkUrl", DbType.String, model.LinkUrl),
                db.CreateInSqlParameter("@IsCheck", DbType.Boolean, model.IsCheck),
                db.CreateInSqlParameter("@LinkID", DbType.Int32, model.LinkID)};

            return db.ExecuteNonQuery(CommandType.Text, strSQL.ToString(), cmdParms) > 0;
        }

        /// <summary>
        /// 删除友情链接
        /// 
        /// 创建: 邱柯皓 2009/02/04 10:38
        /// </summary>
        /// <param name="LinkID">友情链接编号</param>
        /// <returns>返回成功标识</returns>
        public bool Delete(int LinkID)
        {
            string strSQL = "DELETE FROM [Yom_Link] WHERE [LinkID]=@LinkID";
            SqlParameter[] cmdParms = { db.CreateInSqlParameter("@LinkID", DbType.Int32, LinkID) };

            return db.ExecuteNonQuery(CommandType.Text, strSQL, cmdParms) > 0;
        }

        /// <summary>
        /// 获取友情链接实体对象
        /// 
        /// 创建: 邱柯皓 2009/03/12 10:04
        /// </summary>
        /// <param name="LinkID">友情链接编号</param>
        /// <returns>返回友情链接实体对象</returns>
        public Yom.Model.Link GetModel(int LinkID)
        {
            string strSQL = "SELECT * FROM [Yom_Link] WHERE [LinkID]=" + LinkID.ToString();
            using (SqlDataReader dr = db.ExecuteReader(CommandType.Text, strSQL, null))
            {
                Yom.Model.Link model = new Yom.Model.Link();
                while (dr.Read())
                    model = GetModel(dr);

                return model;
            }
        }

        /// <summary>
        /// 获取友情链接列表
        /// 
        /// 创建: 邱柯皓 2009/02/04 10:43
        /// </summary>
        /// <returns>返回友情链接列表</returns>
        public List<Yom.Model.Link> GetList()
        {
            string strSQL = "SELECT * FROM [Yom_Link]";

            using (SqlDataReader dr = db.ExecuteReader(CommandType.Text, strSQL, null))
            {
                List<Yom.Model.Link> list = GetList(dr);

                return list;
            }
        }

        /// <summary>
        /// 修改链接显示状态
        /// 
        /// 创建: 邱柯皓 2009/03/12 10:09
        /// </summary>
        /// <param name="LinkID">链接编号</param>
        /// <param name="State">显示状态</param>
        /// <returns>返回成功标识</returns>
        public bool SetCheck(int LinkID, bool State)
        {
            Yom.Model.Link model = GetModel(LinkID);
            model.IsCheck = State;

            return Update(model);
        }
        #endregion
    }
}